package com.well.kernel.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Copyright &copy; Well All rights reserved.
 * Author:Well
 * Date:2017-11-24
 * Description:链表利用类
 */

public class ListUtil<T> {
    private static Logger logger=Logger.getLogger(ListUtil.class);

    private ListUtil(){}

    /**
     * 截取list
     * @param list list数据
     * @param pageNum 当前页的页码
     * @param pageSize 每一页的数量
     * @param <T>
     * @return
     */
    public static<T> List<T> subList(List<T> list, int pageNum, int pageSize){
        List<T> subList=new ArrayList<T>();
        int total=list.size(); // 总记录
        int pages=(total+pageSize-1)/pageSize; // 页数
        if(pageNum<=pages){
            int fromIndex=(pageNum-1)*pageSize; // list中开始记录的索引
            int toIndex=Math.min(total, pageNum*pageSize); // list中结束记录的索引
            subList=list.subList(fromIndex, toIndex);
        }else{
            logger.warn("当前页码不能大于当前页数");
        }
        return subList;
    }

    public static<T> PageInfo<T> pagination(List<T> list,int pageNum,int pageSize)
    {
        PageInfo<T> pageInfo=new PageInfo<T>(list);
        return pageInfo;
    }

    public static void main(String[] args){
        try {
            ObjectMapper mapper=new ObjectMapper();
            String s=mapper.writeValueAsString(new PageInfo<String>());
            System.out.println(s);
        }catch (Exception e){
            logger.error(e);
        }
    }
}
